package com.hemerasolutions.v2trainer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hemerasolutions.v2trainer.dao.ExceptionDao;
import com.hemerasolutions.v2trainer.dao.interfaces.TrainingDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.TrainingTypeDaoInterface;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.service.interfaces.TrainingSystemServiceInterface;


@Service("tsService")
public class TrainingSystemService extends BaseService implements TrainingSystemServiceInterface {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TrainingDaoInterface tsDao;

	@Autowired
	private TrainingTypeDaoInterface ttDao;
	
	public TrainingSystemService() {
		super();
	}
	
	@PostConstruct
	public void init(){
	}

	@Override
	public Training saveTrainingSystem(Training trainingSystem) {
		try {

			trainingSystem = tsDao.save(trainingSystem);

		} catch (ExceptionDao e) {
			e.printStackTrace();
			throw new ExceptionService(e);
		} catch (ExceptionService e) {
			throw e;
		}
	
		return trainingSystem;
	}

	@Override
	public Training createTrainingSystem(Training trainingSystem) {
		try {
			trainingSystem = tsDao.create(trainingSystem);

		} catch (ExceptionDao e) {
			e.printStackTrace();
			throw new ExceptionService(e);
		} catch (ExceptionService e) {
			throw e;
		}
	
		return trainingSystem;
	}

	
	@Override
	public Training readTrainingSystem(Training trainingSystem) {
		return tsDao.getById(trainingSystem.getId());
	}

	@Override
	public void deleteTrainingSystem(Training trainingSystem) {
		try {
			tsDao.delete(trainingSystem);
		} catch (ExceptionDao e) {
			e.printStackTrace();
			throw new ExceptionService(e);
		}
		
	}

	@Override
	public List<Training> searchTrainingSystems(Map<String, Object> fields) {

		return tsDao.searchTraining(fields);
			
	}

	@Override
	public List<TrainingType> listTrainingTypes() {
		return this.ttDao.list();
	}
		
	
}
